// *************************************************************************************************
//
// MK3D Engine
// --------------------------------------
// Copyright (C) 2007-2011 Zeb (mailto: zebbey@gmail.com)
//
//
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
//
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
//
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
//
// *************************************************************************************************

#include "mk_header.h"
#include "mk_transformation.h"

namespace MK3DE
{
	CTransformation::CTransformation() : 
		_dirty(FALSE),
		_relativeTranslation(0, 0, 0), 
		_relativeRotation(0, 0, 0, 1.0f), 
		_relativeScale(1.0f, 1.0f, 1.0f),
		_relativeScaleRotation(0.0f, 0.0f, 0.0f, 1.0f)
	{
		D3DXMatrixIdentity(&_absoluteTransform);
		D3DXMatrixIdentity(&_relativeTransform);
	}

	CTransformation::~CTransformation()
	{

	}

	void CTransformation::SetRelativeTransformation(D3DXVECTOR3& translation, D3DXQUATERNION& rotation, D3DXVECTOR3& scale)
	{
		_relativeTranslation = translation;
		_relativeRotation = rotation;
		_relativeScale = scale;
		_dirty = TRUE;
	}

	void CTransformation::SetRelativeTranslation(D3DXVECTOR3& translation)
	{
		_relativeTranslation = translation;
		_dirty = TRUE;
	}

	void CTransformation::SetRelativeRotation(D3DXQUATERNION& rotation)
	{
		_relativeRotation = rotation;
		_dirty = TRUE;
	}

	void CTransformation::SetRelativeRotationYawPitchRoll(float yaw, float pitch, float roll)
	{
		D3DXQuaternionRotationYawPitchRoll(&_relativeRotation, yaw, pitch, roll);
		_dirty = TRUE;
	}

	void CTransformation::SetRelativeScale(D3DXVECTOR3& scale)
	{
		_relativeScale = scale;
		_dirty = TRUE;
	}

	void CTransformation::SetRelativeScaleRotation(D3DXQUATERNION& sr)
	{
		_relativeScaleRotation = sr;
		_dirty = TRUE;
	}

	BOOL CTransformation::UpdateAbsoluteTransformation(D3DXMATRIX* pMatParent)
	{
		BOOL dirty = _dirty;
		if (_dirty)
			__UpdateTransformation();

		if (pMatParent)
			_absoluteTransform = _relativeTransform * (*pMatParent);
		else if (dirty)
			_absoluteTransform = _relativeTransform;

		return dirty;
	}

	void CTransformation::__UpdateTransformation()
	{
		D3DXMatrixTransformation(&_relativeTransform,
			&D3DXVECTOR3(0, 0, 0),
			&_relativeScaleRotation,
			&_relativeScale,
			&D3DXVECTOR3(0, 0, 0),
			&_relativeRotation,
			&_relativeTranslation);
		_dirty = FALSE;
	}
}
